package com.cy.store.service.impl;

import com.cy.store.mapper.AddressMapper;
import com.cy.store.pojo.Address;
import com.cy.store.service.IAddressService;
import com.cy.store.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

/**
 * @PROJECT_NAME: store
 * @DESCRIPTION:
 * @USER: 28416
 * @DATE: 2022/11/5 12:59
 */
@Service
public class AddressServiceImpl  implements IAddressService {

    @Value("${user.address.max-count}")
    private  int maxCount;

    @Autowired
    AddressMapper addressMapper;
    //在添加用户的收货地址的业务层，为了完整的把数据添加上去，下拉框的数据要依赖于DistrictService的业务层接口
    @Autowired
    DistrictServiceImpl districtService;
    /**
     *
     * @param address
     * @param uid
     * @param username
     */

    @Override
    public void addNewAddress(Address address, Integer uid, String username) {
        Integer integer = addressMapper.countByUid(uid);
        if (integer >=  maxCount) {
            throw  new AddressCountLimitException("收货地址超过20条");

        }
        //优化代码  将address的数据进行补全

        String provinceName = districtService.getNameByCode(address.getProvinceCode());
        String cityName= districtService.getNameByCode(address.getCityCode());
        String areaName= districtService.getNameByCode(address.getAreaCode());
        address.setProvinceName(provinceName);
        address.setCityName(cityName);
        address.setAreaName(areaName);


        //uid  isDefault
        address.setUid(uid);
        Integer isDefault = integer == 0 ? 1:0;
        address.setIsDefault(isDefault);
        //补齐4个日志
        address.setCreatedUser(username);
        address.setCreatedTime(new Date());
        address.setModifiedUser(username);
        address.setModifiedTime(new Date());

        Integer insert = addressMapper.insert(address);
        if (insert != 1) {
            throw  new InsertException("新增收货地址失败");
        }
    }

    @Override
    public List<Address> getByUid(Integer uid) {

        List<Address> result = addressMapper.findByUid(uid);
        for (Address address : result) {
            //优化 不需要用到的设置为null
//            address.setAid(null);
//            address.setUid(null);
            address.setProvinceCode(null);
            address.setCityCode(null);
            address.setAreaCode(null);
            address.setTel(null);
            address.setCreatedTime(null);
            address.setIsDefault(null);
            address.setCreatedUser(null);
            address.setModifiedTime(null);
            address.setModifiedUser(null);
        }
        return result;
    }

    /**
     * 更设置用户收货地址的默认收货的地址
     * @param aid
     * @param uid
     * @param username
     */
    @Override
    public void setDefault(Integer aid, Integer uid, String username) {
        //1.首先判断此收货地址的数据是否存在
        Address byAid = addressMapper.findByAid(aid);
        if (byAid == null) {
            throw new AddressNotFoundException("找不到这条收货地址");
        }
        //非法访问 检测当前的收货地址的归属
        if (!uid.equals(byAid.getUid())){
            throw  new AccessDeniedException("访问数据非法");
        }
        //2.将所有的地址数据的id_default为0
        Integer integer = addressMapper.updateNonDefault(uid);
        if (integer < 1) {
            throw new UpdateException("更新数据产生位置的异常");
        }
        //3.将用户选择的某条地址设置为默认的收货地址
        Integer integer1 = addressMapper.updateDefaultByAid(aid, username, new Date());
        if (integer1 != 1) {
            throw new UpdateException("更新时产生了位置的异常");
        }
    }

    /**
     * 删除某条收货地址
     * @param uid
     * @param aid
     * @param username
     */
    @Override
    public void deleteAddress(Integer uid, Integer aid, String username) {
        //首先判断是否存在此条数据
        Address byAid = addressMapper.findByAid(aid);
        if (byAid == null) {
            throw  new AddressNotFoundException("收货地址不存在");
        }
        //非法访问 检测当前的收货地址的归属
        if (!uid.equals(byAid.getUid())){
            throw  new AccessDeniedException("访问数据非法");
        }

        //删除数据
        Integer integer = addressMapper.deleteByAid(aid);
        if (integer == null|| integer !=1) {
            throw new DeleteException("删除时产生异常");
        }
        //重新设置默认地址
        Integer integer1 = addressMapper.countByUid(uid);
        if (integer  == 0) {
            //终止程序
            return;
        }

        if (byAid.getIsDefault() == 0){
            return;
        }
        //重新设置默认地址
        Address lastModified = addressMapper.findLastModified(uid);
        Integer integer2 = addressMapper.updateDefaultByAid(lastModified.getAid(), username, new Date());
        if (integer2 != 1) {
                throw  new UpdateException("更新时产生未知异常");
        }



    }

    /**
     * 通过aid查询address
     * @param aid
     * @param uid
     * @return
     */
    @Override
    public Address getByAid(Integer aid,Integer uid) {
        Address byAid = addressMapper.findByAid(aid);
        if(byAid == null){
            throw new AddressNotFoundException("收货地址数据不存在");
        }
        if (!byAid.getUid().equals(uid)){
            throw new AccessDeniedException("访问数据非法");
        }
        //
        byAid.setProvinceCode(null);
        byAid.setCityCode(null);
        byAid.setAreaCode(null);

        byAid.setCreatedTime(null);
        byAid.setCreatedUser(null);
        byAid.setModifiedUser(null);
        byAid.setModifiedTime(null);
        return byAid;
    }
}
